package com.txd.mofa.wode.fgt;

import android.Manifest;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.kongzue.baseframework.interfaces.Layout;
import com.kongzue.baseframework.util.AppManager;
import com.kongzue.baseframework.util.JumpParameter;
import com.kongzue.baseframework.util.OnPermissionResponseListener;
import com.kongzue.baseframework.util.Preferences;
import com.kongzue.baseokhttp.HttpRequest;
import com.kongzue.baseokhttp.listener.ResponseListener;
import com.kongzue.baseokhttp.util.Parameter;
import com.ninetaildemonfox.zdl.ninetails.map.JSONUtils;
import com.txd.mofa.R;
import com.txd.mofa.base.BaseFgt;
import com.txd.mofa.login.BangdingAty;
import com.txd.mofa.login.LoginAty;
import com.txd.mofa.login.StartAty;
import com.txd.mofa.net.NetUtils;
import com.txd.mofa.utils.CleanDataUtils;
import com.txd.mofa.utils.TextviewJiazaiHtml;
import com.txd.mofa.utils.UpdateService;
import com.txd.mofa.view.GengxinDialog;
import com.txd.mofa.view.ShanchuDialog;
import com.txd.mofa.wode.aty.GuanyuAty;
import com.txd.mofa.wode.aty.NichengAty;
import com.txd.mofa.wode.aty.ShenqingWebAty;
import com.txd.mofa.wode.aty.TouxiangAty;
import com.txd.mofa.wode.aty.WebAty;
import com.txd.mofa.wode.aty.Wodezuji;

import java.util.ArrayList;
import java.util.Map;

@Layout(R.layout.fgt_wo)
public class WoFgt extends BaseFgt {

    private LinearLayout ll_touxiang,ll_huancun,ll_nicheng,ll_guanyu,ll_shouji,ll_banquan,ll_xieyi,ll_yinsi,ll_gengxin,ll_hcuangzuozhe,ll_zuji;
    private TextView tv_tuichu,tv_nicheng,tv_shoujihao,tv_huancun,tv_gengxin;
    private ImageView iv_touxiang;
    private Map<String, String> data;
    private int i;

    public static WoFgt newInstance() {

        Bundle args = new Bundle();

        WoFgt fragment = new WoFgt();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initViews() {
        super.initViews();
        ll_hcuangzuozhe = findViewById(R.id.ll_chuangzuozhe);
        ll_zuji = findViewById(R.id.ll_zuji);
        ll_gengxin = findViewById(R.id.ll_gengxin);
        iv_touxiang = findViewById(R.id.iv_touxiang);
        tv_nicheng = findViewById(R.id.tv_nicheng);
        tv_shoujihao = findViewById(R.id.tv_shoujihao);
        tv_huancun = findViewById(R.id.tv_huancun);
        tv_gengxin = findViewById(R.id.tv_gengxin);
        ll_banquan = findViewById(R.id.ll_banquan);
        ll_xieyi = findViewById(R.id.ll_xieyi);
        ll_yinsi = findViewById(R.id.ll_yinsi);
        ll_guanyu = findViewById(R.id.ll_guanyu);
        ll_shouji = findViewById(R.id.ll_shouji);
        ll_nicheng = findViewById(R.id.ll_nicheng);
        ll_huancun = findViewById(R.id.ll_huancun);
        tv_tuichu = findViewById(R.id.tv_tuichu);
        ll_touxiang = findViewById(R.id.ll_touxiang);
    }

    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {

        http();

    }

    @Override
    protected void onNetworkDisConnected() {

    }

    @Override
    public void onResume() {
        super.onResume();
        http();
        try {
            String totalCacheSize = CleanDataUtils.getTotalCacheSize(me);
            tv_huancun.setText(totalCacheSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void http() {
        HttpRequest.POST(me, woindex, new Parameter().add("token",token), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                if (error == null) {
                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                    if (map.get("code").equals("1")){
                        data = JSONUtils.parseKeyAndValueToMap(map.get("data"));

                        TextviewJiazaiHtml.setFuzhiText(tv_nicheng,data.get("nickname"));

                        tv_shoujihao.setText(data.get("account"));

                        Glide.with(me).load(data.get("head_pic")).into(iv_touxiang);


                        Preferences.getInstance().set(me,"user_id","user_id",data.get("id"));

                        Preferences.getInstance().set(me,"tongyi","tongyi",Integer.parseInt(data.get("agree")));

                        Preferences.getInstance().set(me,"shouji","shouji",data.get("account"));

                    }


                }else {
                    toast("网络异常");
                }
            }
        });
    }

    @Override
    public void onLazyLoad() {

    }

    @Override
    public void setEvents() {

        ll_hcuangzuozhe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetUtils.isNetworkAvailable(me)){
                    jump(ShenqingWebAty.class, new JumpParameter().put("name", "申请创作者"));
                }else {
                    Toast.makeText(me, "网络连接错误，请检查您的网络", Toast.LENGTH_SHORT).show();
                }
            }
        });

        ll_zuji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetUtils.isNetworkAvailable(me)){
                    jump(Wodezuji.class,new JumpParameter().put("name","我的足迹"));
                }else {
                    Toast.makeText(me, "网络连接错误，请检查您的网络", Toast.LENGTH_SHORT).show();
                }
            }
        });

        ll_xieyi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetUtils.isNetworkAvailable(me)){
                    jump(WebAty.class,new JumpParameter().put("name","用户协议"));
                }else {
                    Toast.makeText(me, "网络连接错误，请检查您的网络", Toast.LENGTH_SHORT).show();
                }
            }
        });

        ll_banquan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetUtils.isNetworkAvailable(me)){
                    jump(WebAty.class,new JumpParameter().put("name","版权说明"));
                }else {
                    Toast.makeText(me, "网络连接错误，请检查您的网络", Toast.LENGTH_SHORT).show();
                }
            }
        });

        ll_yinsi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetUtils.isNetworkAvailable(me)){
                    jump(WebAty.class,new JumpParameter().put("name","隐私政策"));
                }else {
                    Toast.makeText(me, "网络连接错误，请检查您的网络", Toast.LENGTH_SHORT).show();
                }
            }
        });

        ll_guanyu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetUtils.isNetworkAvailable(me)){
                    jump(GuanyuAty.class);
                }else {
                    Toast.makeText(me, "网络连接错误，请检查您的网络", Toast.LENGTH_SHORT).show();
                }

            }
        });

        ll_shouji.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetUtils.isNetworkAvailable(me)){
                    if (data != null) {
                        if (data.get("account").equals("")) {
                            jump(BangdingAty.class, new JumpParameter().put("name", "绑定手机号"));
                        } else {
                            jump(BangdingAty.class, new JumpParameter().put("name", "更换手机号"));
                        }
                    }
                }else {
                    Toast.makeText(me, "网络连接错误，请检查您的网络", Toast.LENGTH_SHORT).show();
                }
            }
        });
        ll_nicheng.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetUtils.isNetworkAvailable(me)){
                    jump(NichengAty.class,new JumpParameter().put("nicheng",tv_nicheng.getText().toString()));
                }else {
                    Toast.makeText(me, "网络连接错误，请检查您的网络", Toast.LENGTH_SHORT).show();
                }

            }
        });

        tv_tuichu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (NetUtils.isNetworkAvailable(me)){
                    ShanchuDialog shanchuDialog = new ShanchuDialog(me, "确认要退出账号吗？",new ShanchuDialog.SignListener() {
                        @Override
                        public void shanchu() {
                            AppManager.getInstance().killAllActivity();
                            Preferences.getInstance().cleanAll(me,"mofa");
                            jump(LoginAty.class);
                        }
                    });
                    shanchuDialog.show();
                }else {
                    Toast.makeText(me, "网络连接错误，请检查您的网络", Toast.LENGTH_SHORT).show();
                }

            }
        });
        ll_huancun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShanchuDialog shanchuDialog = new ShanchuDialog(me, "您确认要清除缓存？",new ShanchuDialog.SignListener() {
                    @Override
                    public void shanchu() {
                        CleanDataUtils.clearAllCache(me);
                        tv_huancun.setText("0.00K");
                    }
                });
                shanchuDialog.show();
            }
        });
        ll_touxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetUtils.isNetworkAvailable(me)){
                    jump(TouxiangAty.class,new JumpParameter().put("url",data.get("head_pic")));
                }else {
                    Toast.makeText(me, "网络连接错误，请检查您的网络", Toast.LENGTH_SHORT).show();
                }

            }
        });

        ll_gengxin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (NetUtils.isNetworkAvailable(me)){
                    i = packageCode(me);
                    http1();
                }else {
                    Toast.makeText(me, "网络连接错误，请检查您的网络", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }
    public static int packageCode(Context context) {
        PackageManager manager = context.getPackageManager();
        int code = 0;
        try {
            PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
            code = info.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return code;
    }
    private void http1() {
        HttpRequest.POST(me, updateAndroid, new Parameter(), new ResponseListener() {
            @Override
            public void onResponse(String response, Exception error) {
                if (error == null) {
                    Map<String, String> map = JSONUtils.parseKeyAndValueToMap(response);
                    if (map.get("code").equals("1")){
                        data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                        if (Integer.parseInt(data.get("versionCode")) > i){

                            GengxinDialog dialog = new GengxinDialog(me, data,new GengxinDialog.SignListener() {
                                @Override
                                public void shengji() {

                                    requestPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, new OnPermissionResponseListener() {
                                        @Override
                                        public void onSuccess(String[] permissions) {

                                            UpdateService updateService = new UpdateService(me);
                                            updateService.download(data.get("url"),System.currentTimeMillis()+".apk");

                                        }

                                        @Override
                                        public void onFail() {

                                        }
                                    });

                                }
                            });
                            dialog.show();
                        }else {
                            Toast.makeText(me, "已是最新版本", Toast.LENGTH_SHORT).show();
                        }
                    }
                }else {
                    toast("网络异常");
                }
            }
        });
    }

}
